package com.qinggeng.framework.config.mq.impl;

import com.alibaba.fastjson.JSONObject;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.framework.config.mq.dto.MessageGoodsKeepReduceDTO;
import com.qinggeng.framework.config.mq.handle.MessageHandle;
import com.qinggeng.project.system.service.IGoodsKeepService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 吴鹏飞
 * @date 2020/12/15
 */
@Component
@Slf4j
public class GoodsKeepReduceHandleImpl implements MessageHandle {
    @Autowired
    private IGoodsKeepService goodsKeepService;

    @Override
    public void handle(String msg) {
        try {
            MessageGoodsKeepReduceDTO messageGoodsKeepReduceDTO = JSONObject.parseObject(msg, MessageGoodsKeepReduceDTO.class);
            int i = goodsKeepService.subKeepNum(messageGoodsKeepReduceDTO.getGoodsId(), messageGoodsKeepReduceDTO.getShopId(), messageGoodsKeepReduceDTO.getKeepNum(), messageGoodsKeepReduceDTO.getGoodsNum());
            if (i != 1){
                log.error("扣减库存失败GoodsKeepReduceHandleImpl.handle i={}",i);
                throw new CustomException("扣减库存失败");
            }
        } catch (CustomException e) {
            log.error(e.getMessage(),e);
        }
    }
}
